<?php

// @formatter:off
/**
 * @file ct.translate.update.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: ct.translate.update.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/cron/ct.translate.update.php $
 */
// @formatter:on

class WCTranslateUpdate extends WCCronTool {
	public function run($spacer = "") {
		global $wcOptions;

		WCTranslate::I()->update(true, $spacer);

		if($wcOptions->activated("Compare")) {
			$this->compare($spacer);
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}This tool updates language translation keys by loading all language XMLs.\n\n";
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-c, --compare\n";
		echo "{$spacer}\t\t@TODO\n\n";
	}
	//
	// Protected methods.
	protected function compare($spacer) {
		$db = WCConnection::Instance();

		$translations = WCTranslate::I();
		$langs = $translations->langs();

		$stmtDiffId = __CLASS__."::".__FUNCTION__."[DIFF]";
		if(!$db->has($stmtDiffId)) {
			$query = "select  ltr_name\n";
			$query.= "from    {$db->prefix()}lang_translations\n";
			$query.= "group by ltr_name\n";
			$query.= "having count(*) < :count\n";

			$db->prepare($stmtDiffId, $query);
		}

		$stmt = $db->get($stmtDiffId);
		$params = array(
			":count" => count($langs)
		);
		if($stmt->execute($params) && $stmt->rowCount() > 0) {
			echo "{$spacer}Unbalanced translations:\n";
			sort($langs);
			foreach($stmt->fetchAll() as $row) {
				$name = $row["ltr_name"];
				echo "{$spacer}\t'{$name}':\n";
				foreach($langs as $lang) {
					$trans = $translations->translateFor($lang, $name);
					echo "{$spacer}\t\t[{$lang}] ";
					if($trans != "@{$name}") {
						echo "'".WCColorText::ShellGreen("{$trans}")."'\n";
					} else {
						echo WCColorText::ShellRed("[Failed]\n");
					}
				}
			}
		}
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addOption("Compare")) {
				$wcOptions->addOptionTrigger("Compare", "c");
				$wcOptions->addOptionTrigger("Compare", "compare");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
}

?>